# ip存储与读取文件统计ip频率 (京东二面题)
# • 生成一个大文件ips.txt,要求120000行，每行随即为172.25.254.0/24段的ip;
# • 读取ips.txt文件统计这个文件中ip出现频率前10的ip；
import random
from typing import List

filename = "ips.txt"
ips_dict = dict()
with open(filename, "w", encoding="UTF-8") as file:
    for i in range(120000):
        file.write("172.25.254." + str(random.randint(0, 255)) + "\n")
with open(filename, "r", encoding="UTF-8") as files:
    ips = {}
    for line in files:
        if line in ips:
            ips[line] += 1
        else:
            ips[line] = 1


def sorted_count_ip(filename, count=10):
    ips_dict = dict()
    with open(filename) as f:
        for ip in f:
            if ip not in ips:
                ips[ip] = 1
            else:
                ips[ip] += 1


from collections import Counter


def sort_by_ip_count(filename='ips.txt'):
    with open(filename) as f:
        ipcount = Counter(f)
    return [ip[0].strip() for ip in ipcount.most_common(10)]


# create_file()
print(sort_by_ip_count())
